package com.csyf.manager.service.impl;

import com.csyf.manager.dao.SysPayeeDao;
import com.csyf.manager.entity.SysPayee;
import com.csyf.manager.exception.ApiValidateException;
import com.csyf.manager.service.SysPayeeBiz;
import com.csyf.manager.utils.page.JqGridVo;
import com.csyf.manager.utils.page.PageUtils;
import com.github.pagehelper.PageHelper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

/**
 * @Author: casper
 * @Description: 常用出款人
 * @Date: Created in 2018/9/17 9:56
 */
@Slf4j
@Service
public class SysPayeeBizImpl implements SysPayeeBiz {

    @Autowired
    private SysPayeeDao sysPayeeDao;


    @Override
    public List<SysPayee> queryAll() {
        return sysPayeeDao.selectAll();
    }

    /**
     * 查询常用出款人列表
     *
     * @param vo
     * @param pageIndex
     * @param pageSize
     * @return
     */
    @Override
    public JqGridVo querydata(SysPayee vo, Integer pageIndex, Integer pageSize) {
        PageHelper.startPage(pageIndex, pageSize);
        PageHelper.orderBy("id desc");

        Example example = new Example(SysPayee.class);
        Example.Criteria criteria = example.createCriteria();
        PageUtils.andLikeTo(criteria, "accountName", vo.getAccountName());
        PageUtils.andLikeTo(criteria, "cardNum", vo.getCardNum());

        List<SysPayee> select = sysPayeeDao.selectByExample(example);
        return PageUtils.pasrJqGrid(select);
    }

    /**
     * 保存常用出款人信息
     *
     * @param vo
     */
    @Override
    public void savePayee(SysPayee vo) throws ApiValidateException {
        SysPayee sysPayee = new SysPayee();
        sysPayee.setCardNum(vo.getCardNum());
        int i = sysPayeeDao.selectCount(sysPayee);
        if (i > 0) {
            throw new ApiValidateException("卡号已存在");
        }
        sysPayeeDao.insertSelective(vo);

    }

    /**
     * 根据Id查询出款人信息
     *
     * @param payeeId
     * @return
     */
    @Override
    public SysPayee queryPayeeById(String payeeId) {
        return sysPayeeDao.selectByPrimaryKey(payeeId);
    }

    /**
     * 修改出款人信息
     *
     * @param vo
     * @return
     */
    @Override
    public int updatePayeeById(SysPayee vo) {
        return sysPayeeDao.updateByPrimaryKeySelective(vo);
    }

    /**
     * 删除出款人信息
     *
     * @param payeeId
     */
    @Override
    public void deletePayeeById(Integer payeeId) {
        sysPayeeDao.deleteByPrimaryKey(payeeId);
    }
}
